﻿using System;
using System.Collections.Generic;
using System.IO;
using Com.Lancetinc.Evs.Data.Domain;
using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.ViewModels;

namespace Com.Lancetinc.Evs.Services
{
    public class ExperInfo : ViewModelBase
    {
        public ExperInfo()
        {
            Exper = new ExperViewModel(new Exper());
            Images = new SortedObservableCollection<string>();
        }

        public ExperInfo(ExperViewModel exper)
        {
            Exper = exper;
            Images = new SortedObservableCollection<string>();
        }

        private string _workDir;
        public string WorkDir
        {
            get => _workDir;
            set => SetProperty(ref _workDir, value);
        }

        private string _pictureDir;
        public string PictureDir
        {
            get => _pictureDir;
            set => SetProperty(ref _pictureDir, value);
        }

        private string _videoName;
        public string VideoName
        {
            get => _videoName;
            set => SetProperty(ref _videoName, value);
        }

        private ExperViewModel _exper;
        public ExperViewModel Exper
        {
            get => _exper;
            set => SetProperty(ref _exper, value);
        }

        public SortedObservableCollection<string> Images { get; }

        public string VideoPath => Path.Combine(WorkDir, VideoName);

        public void Reset()
        {
            WorkDir = null;
            PictureDir = null;
            VideoName = null;
            Exper.Reset();
            Images.Clear();

            this.Refresh();
        }

        public void DeleteImages(List<string> images)
        {
            foreach (var name in images)
            {
                if (Images.Contains(name))
                {
                    File.Delete(GetImageFullPath(name));
                    Images.Remove(name);
                }
            }
        }

        public DateTime GetStartTime()
        {
            if (string.IsNullOrEmpty(Exper.Exper.StartTime))
            {
                return DateTime.MinValue;
            }

            return DateTime.Parse(Exper.Exper.StartTime);
        }

        public string GetImageFullPath(string imageName)
        {
            return Path.Combine(PictureDir, imageName);
        }

        public bool HasVideo()
        {
            return File.Exists(VideoPath);
        }

        public bool HasImage()
        {
            return Images.Count > 0;
        }
    }
}